﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Club
{
    public partial class MyBookings : System.Web.UI.Page
    {
        BLL.ManageBookings manageBookings = new BLL.ManageBookings();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, object> sessionVar = (Dictionary<string, object>)Session["vars"];
                DAL.User user = (DAL.User)sessionVar["userVar"];
                if (user == null)
                {
                    Response.Redirect("Default.aspx?prepage=MyBookings.aspx");
                }

                BLL.BookFacility bookFacility = new BLL.BookFacility();
                List<DAL.Booking> bookings = new List<DAL.Booking>();
                bookings = manageBookings.getBookingsByUser(((DAL.User)((Dictionary<string, object>)Session["vars"])["userVar"]));

                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn());
                dt.Columns.Add(new DataColumn());
                dt.Columns.Add(new DataColumn());
                dt.Columns.Add(new DataColumn());
                dt.Columns[0].ColumnName = "Id";
                dt.Columns[1].ColumnName = "FacilityName";
                dt.Columns[2].ColumnName = "BookingDate";
                dt.Columns[3].ColumnName = "BookingTime";

                foreach (DAL.Booking b in bookings)
                {
                    DataRow tr = dt.NewRow();
                    tr["Id"] = b.Id.ToString();
                    tr["FacilityName"] = bookFacility.getFacility(Convert.ToInt32(b.FacilityId)).Name;
                    tr["BookingDate"] = ((DateTime)b.BookingDate).ToShortDateString();
                    tr["BookingTime"] = b.BookingTime;
                    dt.Rows.Add(tr);

                }
                GridView2.DataSource = dt;
                GridView2.DataBind();
                GridView2.Columns[0].Visible = false;

                if (Request.QueryString["msg"] != null)
                {
                    Label1.Text = Request.QueryString["msg"].ToString();
                }
            }
            catch (Exception ex)
            {
                WebCommon.ShowMsg(Response, ex.Message);
            }           

        }

        protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            DAL.Booking booking = new DAL.Booking();
            DataTable dt = (DataTable)GridView2.DataSource;
            booking.Id = Convert.ToInt16(dt.Rows[Convert.ToInt32(e.CommandArgument)]["Id"].ToString());
            manageBookings.cancelBooking(booking);
            Response.Redirect("~/MyBookings.aspx?msg=Booking has been canceled");
            
        }
    }
}